﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Demo
{
    public partial class EditableStoreDemo : Form
    {
        public EditableStoreDemo()
        {
            InitializeComponent();
            store.IsAsc = true;
            store.OnDataChanged += store_OnDataChanged;
        }

        void store_OnDataChanged(object data, object staticData, int pageNumber, int pageSize, int staticPageNumber, int staticPageSize)
        {
            dgv.DataSource = data;
        }
        Cocon90.Lib.Dal.Store.EditableStore<Model.Address> store = new Cocon90.Lib.Dal.Store.EditableStore<Model.Address>();
        private void 加载数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            store.Load();
        }

        private void 上一面ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            store.PageNumber--;
            store.Load();
        }

        private void 下一页ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            store.PageNumber++;
            store.Load();
        }

        private void dgv_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show(e.Exception.Message);
            e.Cancel = true;
        }

        private void 取得更改项ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var change = store.GetChanged();
            
            MessageBox.Show("发生更改的行:" + change.Count);
        }

        private void 更新到数据库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ret = store.ApplyChange();
            MessageBox.Show("执行更新数：" + ret.AllCount + "\r\n成功数：" + ret.SuccessCount + "\r\n失败数：" + ret.FailCount);
        }
    }
}
